package qualomelhor.game;

import static playn.core.PlayN.graphics;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;

public class Braco {

	public static void drawBezierCurve(ImageLayer[] imgL, int xOrigem, int yOrigem, int xDest, int yDest, int bezierX, int bezierY) {

		int i = 0;
		for(double t=0.0; i < 11;t+=0.1, i++) {  
		    int x = (int) (  (1-t)*(1-t)*xOrigem + 2*(1-t)*t*bezierX+t*t*xDest);  
		    int y = (int) (  (1-t)*(1-t)*yOrigem + 2*(1-t)*t*bezierY+t*t*yDest);  

		    float xtmp = x - (imgL[i].width() / 2);
		    float ytmp = y - (imgL[i].height() / 2);

		    imgL[i].setTranslation(xtmp , ytmp);
		} 
	}

	private GroupLayer gLayerBraco;
	private ImageLayer[] layer;
	private Image imgBraco;
	private float posXIni;
	private float posYIni;
	private float posXFim;
	private float posYFim;
	private float angle;
	private int numImages;
	private int bezierX;
	private int bezierY;

	public Braco (int numImages, float xIni, float yIni, float xFim, float yFim, float angle, Image imgBraco) {
		this.numImages = numImages;
		this.imgBraco = imgBraco;
		this.posXIni = xIni;
		this.posYIni = yIni;
		this.posXFim = xFim;
		this.posYFim = yFim;
		layer = new ImageLayer[numImages];
		gLayerBraco = graphics().createGroupLayer();
		for (int i = 0; i < this.numImages; i++) {
			layer[i] = graphics().createImageLayer(imgBraco);
			gLayerBraco.add(layer[i]);
		}
		setVisible(false);
	}

	public void update(float delta) {
		Braco.drawBezierCurve(layer, (int)posXIni, (int)posYIni, (int)posXFim, (int)posYFim, bezierX, bezierY);
		//layer.setRotation(angle);
		//layer.setTranslation(posX, posY);
		//layer.setScale(scaleX, scaleY);
	}

	/*
	public void paint(float alpha) {
		// interpolate based on previous state
		float x = (body.getPosition().x * alpha) + (prevX * (1f - alpha));
		float y = (body.getPosition().y * alpha) + (prevY * (1f - alpha));
		float a = (body.getAngle() * alpha) + (prevA * (1f - alpha));
		//layer.setTranslation(x, y);
		layer.setTranslation(x + (float)((scaleFactorX == -1)?this.width * Math.cos(a):0), y + (float)(this.height * Math.sin(a)));
		//layer.setTranslation(x + getWidth() / 2, y + + getHeight() / 2);
		layer.setRotation(a);
		//layer.setRotation(a + (float)((scaleFactorX == -1)?Math.PI:0f));
		layer.setScale(scaleFactorX * layer.transform().scaleX(), scaleFactorY * layer.transform().scaleY());
	}
	*/

	public GroupLayer getGroupLayer() {
		return gLayerBraco;
	}

	public int getBezierX() {
		return bezierX;
	}

	public int getBezierY() {
		return bezierY;
	}

	public Image getImage() {
		return imgBraco;
	}

	public float getPosXIni() {
		return posXIni;
	}

	public float getPosYIni() {
		return posYIni;
	}

	public float getPosXFim() {
		return posXFim;
	}

	public float getPosYFim() {
		return posYFim;
	}

	public void setImageBraco(int numImages, Image imgBraco) {
		this.numImages = numImages;
		this.imgBraco = imgBraco;
		gLayerBraco.clear();
		layer = new ImageLayer[numImages];
		gLayerBraco = graphics().createGroupLayer();
		for (int i = 0; i < this.numImages; i++) {
			layer[i] = graphics().createImageLayer(imgBraco);
			gLayerBraco.add(layer[i]);
		}
	}

	public void setImageBraco(Image imgBraco) {
		this.imgBraco = imgBraco;
		for (int i = 0; i < this.numImages; i++) {
			layer[i].setImage(imgBraco);
		}
	}

	public void setPosition (float xIni, float yIni, float xFim, float yFim, int bezierX, int bezierY) {
		this.posXIni = xIni;
		this.posYIni = yIni;
		this.posXFim = xFim;
		this.posYFim = yFim;
		this.bezierX = bezierX;
		this.bezierY = bezierY;
	}

	public void setPosition (float xIni, float yIni, float xFim, float yFim) {
		this.posXIni = xIni;
		this.posYIni = yIni;
		this.posXFim = xFim;
		this.posYFim = yFim;
	}

	public void setPositionIni (float xIni, float yIni) {
		this.posXIni = xIni;
		this.posYIni = yIni;
	}

	public void setPositionFim (float xFim, float yFim) {
		this.posXFim = xFim;
		this.posYFim = yFim;
	}

	public void setAngle(float angle) {
		this.angle = angle;
	}

	public void setBezierX(int bezierX) {
		this.bezierX = bezierX;
	}

	public void setBezierY(int bezierY) {
		this.bezierY = bezierY;
	}

	public void setBezier(int bezierX, int bezierY) {
		this.bezierX = bezierX;
		this.bezierY = bezierY;
	}

	public void incrementBezier(int x, int y) {
		this.bezierX += x;
		this.bezierY += y;
	}

	public void setVisible(boolean visible) {
		gLayerBraco.setVisible(visible);
	}
}